//
//  ImagePickerViewController.swift
//  SwiftDemo
//
//  Created by Jason on 15/10/10.
//  Copyright © 2015年 Jason Jiang. All rights reserved.
//

import UIKit
import AssetsLibrary


class ImagePickerViewController: UIViewController,UINavigationControllerDelegate,UIImagePickerControllerDelegate{
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        self.view.backgroundColor = UIColor.grayColor();
        
        let button:UIButton = UIButton (type: UIButtonType.Custom);
        
        button.setTitle("ImgAction", forState: UIControlState.Normal);
        
        button.frame = CGRectMake(50, 80, 40, 40);
        button.backgroundColor = UIColor.redColor()
        ;
        button.addTarget(self, action: Selector("imgAction"), forControlEvents: UIControlEvents.TouchUpInside);
        
        self.view.addSubview(button);
        
        
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    /**
    Jump to imagePickerController
    
    */
    
    func imgAction(){
        
        /**(
        "public.image",
        "public.movie"
        )
        **/
        
        NSLog("the avaliable media Types is %@", UIImagePickerController.availableMediaTypesForSourceType(UIImagePickerControllerSourceType.Camera)!);
        
        let imgP:UIImagePickerController = UIImagePickerController ();
        imgP.delegate = self;
        imgP.sourceType = UIImagePickerControllerSourceType.Camera;
        imgP.mediaTypes = ["public.movie"];
        self.presentViewController(imgP, animated: true) { () -> Void in
            
        }
        
    }
    
    /**
    add img to self.view
    
    */
    func addImg(img:UIImage){
        
        
        let imgV = UIImageView.init(image: img);
        imgV.contentMode = UIViewContentMode.ScaleAspectFit;
        imgV.frame = CGRectMake(60, 90, 200, 200);
        self.view.addSubview(imgV);
        
        
    }
    
    
    /**< imagepicker delegate */
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
        
        
        NSLog("the edit info is %@", editingInfo!);
        
        picker.dismissViewControllerAnimated(true) { () -> Void in
            
        };
    };
    
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]){
        
        
        NSLog("the edit info is %@", info)
        
        let infoDic:NSDictionary = info
        
        let str:NSURL = infoDic.objectForKey("UIImagePickerControllerMediaURL") as! NSURL
        
        //        let url:NSURL = NSURL(string: str as String)!;
        
        let alassertL = ALAssetsLibrary ();
        
        alassertL.writeVideoAtPathToSavedPhotosAlbum(str,completionBlock: {(u:NSURL!, e:NSError!) in
            
            
            NSLog("fasfda");
            
            picker.dismissViewControllerAnimated(true) { () -> Void in
                
            };
            
            
            }
            
        );
        
    }
    
    
    /*
    // MARK: - Navigation
    
    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // Get the new view controller using segue.destinationViewController.
    // Pass the selected object to the new view controller.
    }
    */
    
}
